package com.jeesite.modules.kj.api.controller.file;


import com.aliyun.oss.OSSClient;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.aliyun.oss.bean.AliyunOSS;
import com.jeesite.modules.aliyun.oss.service.impl.OssFileUploadServiceExtendImpl;
import com.jeesite.modules.common.interfaces.CurrentUser;
import com.jeesite.modules.common.interfaces.UserLoginToken;
import com.jeesite.modules.kj.api.controller.activity.ActivityApiController;
import com.jeesite.modules.kj.api.vo.responseVo.ResponseBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;

/**
 * Author:hzy
 * Date:2021/07/04
 * Description:
 */
@Api(value = "文件上传api", tags = {"文件上传api"})
@Slf4j
@Controller
@RequestMapping(value = "/api/file")
public class FileUploadApiController {

    private final static Logger logger = LoggerFactory.getLogger(FileUploadApiController.class);

    @Value("${oss.bucketName}")
    String bucketName;
    @Value("${oss.endPoint}")
    String endPoint;
    @Value("${oss.downloadHost}")
    String downloadHost;


    @Value("${oss.endPoint}")
    private String endpoint;

    @Value("${oss.accessKeyId}")
    private String accessKeyId;

    @Value("${oss.accessKeySecret}")
    private String accessKeySecret;

    OSSClient ossClient;

    @Autowired
    OssFileUploadServiceExtendImpl ossFileUploadServiceExtend;

    @Autowired
    AliyunOSS aliyunOSS;

    @ApiOperation(value = "上传图片", notes = "上传图片")
    @PostMapping(value = "newUpload")
    @ResponseBody
    public ResponseBean<String> newUpload( MultipartFile file) {
        try {
            String name = StringUtils.getRandomStr(32);
            String ext = FilenameUtils.getExtension(file.getOriginalFilename());
            String path = DateUtils.getDate("yyyyMM") + "/";
            String filePath = path + name + "." + ext;
            ossClient = aliyunOSS.getOssClient();
            if(ossClient == null){
                ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            }
            ossFileUploadServiceExtend.uploadFile(ossClient, file.getInputStream(), filePath);
            String url = downloadHost + "/" + filePath;;
            return new ResponseBean<>(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
